<template>
  <div style="padding: 20px">
    <el-form
      v-loading="loading"
      :model="userparams"
      status-icon
      :rules="rules"
      ref="reForm"
      label-width="80px"
      label-position="left"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名：">
        <div>
          <img src="" alt="" />
          <span
            style="
              font-family: Alibaba PuHuiTi-Regular, Alibaba PuHuiTi;
              font-weight: 400;
              color: #000000;
            "
            >{{ userparams.name }}</span
          >
        </div>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="userparams.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="two_pass">
        <el-input
          type="password"
          v-model="userparams.two_pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-button class="btn" @click="handleSave('reForm')">保存</el-button>
    </el-form>
  </div>
</template>
<script>
import { userUpdate } from "@/api/user";
import { validatePassword } from "@/utils/validate";
export default {
  // props:{
  //   type:{
  //     type:Number,
  //     required:true
  //   }
  // },
  data() {
    const revalidatePassword = (rule, value, callback) => {
      if (this.userparams.pass !== value) {
        callback(new Error("两次输入的密码不一直"));
      } else {
        callback();
      }
    };
    return {
      loading: false, //加载状态
      user_login: "",
      userparams: {
        uid: "",
        name: "",
        pass: "",
        two_pass: "",
      },
      rules: {
        pass: [
          { required: true, validator: validatePassword, trigger: "blur" },
        ],
        two_pass: [
          { required: true, validator: revalidatePassword, trigger: "blur" },
        ],
      },
    };
  },

  created() {
    // 获取用户的名字
    console.log(this.user);
    if (this.user) {
      this.userparams.uid = this.user.id;
      this.userparams.name = this.user.user_login;
    }
  },

  props: {
    user: {
      type: Object,
      default: null,
    },
    type: {
      type: Number,
      default: 1,
    },
  },

  methods: {
    handleCancle(type) {
      this.$emit("close", type);
    },
    handleSave() {
      this.$refs.reForm.validate((valid) => {
        if (!valid) return;
        this.loading = true;
        userUpdate(this.userparams).then((res) => {
          if (res.code == 1) {
            this.$message.success("修改成功");
            // 滞空
            this.userparams = {
              uid: "",
              name: "",
              pass: "",
              two_pass: "",
            };
            this.handleCancle(false);
          }else{
            this.$message.error("修改失败");
          }
          this.loading = false;
        });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.demo-ruleForm {
  padding: 20px 20px;
  .btn {
    width: 137px;
    background: #dd8341;
    border-radius: 5px 5px 5px 5px;
    opacity: 1;
    color: #fff;
    font-weight: 700;
    font-size: 16px;
    margin: 10px 44%;
    box-sizing: border-box;
  }
}
</style>